﻿Imports MySql.Data.MySqlClient

Public Class t05_master_user
    Inherits DevExpress.XtraEditors.XtraForm
    Dim acr As New ModLibrary.All_Function_Cls
    Public BS As BindingSource

    Private Sub t05_master_user_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.UserSkins.OfficeSkins.Register()
        Me.LookAndFeel.SkinName = "Office 2010 Blue"
        DefaultLookAndFeel1.LookAndFeel.SkinName = "Office 2010 Blue"
        LoadNow()
    End Sub

    Sub LoadNow()
        BS = Master_userBindingSource
        'TODO: This line of code loads data into the 'DS_t05_master_user.master_user' table. You can move, or remove it, as needed.
        Me.Master_userTableAdapter.Fill(Me.DS_t05_master_user.master_user)
    End Sub

    Function Form_Valid()
        Dim Pesan, Validasi As String
        Pesan = "" : Validasi = ""
        'If ID_USERTextBox.Text = "" Then Pesan += "- ID_User Cannot Empty " & vbNewLine
        If USERNAME.Text = "" Then Pesan += "- Username Tidak Boleh Kosong " & vbNewLine
        If PASSWORD.Text = "" Then Pesan += "- Password Tidak Boleh Kosong " & vbNewLine
        If ROLE.Text = "" Then Pesan += "- Role Tidak Boleh Kosong " & vbNewLine
        If Pesan <> "" Then
            Validasi = "Gagal menyimpan data : " & vbNewLine & vbNewLine & Pesan
        End If
        Return Validasi
    End Function

    Private Sub TS_Refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        LoadNow()
        BS.MoveLast()
    End Sub
    Sub Save()
        Try
            If Form_Valid() = "" Then
                Me.Validate()
                Me.Master_userBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DS_t05_master_user)
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Save_Special()
        Try
            Me.Validate()
            Me.Master_userBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DS_t05_master_user)
            MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
            LoadNow()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Delete()
        If MsgBox("Delete this data ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Confirmation") = MsgBoxResult.Yes Then
            BS.RemoveCurrent()
            If MsgBox("if you are sure... click [Yes] button to delete and save this changes..", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "Pending Delete Data") = MsgBoxResult.Ok Then
                If Form_Valid() = "" Then
                    Save()
                Else
                    Save_Special()
                End If
            Else
                acr.MoveNavi("Load", 0, Me, BS)
            End If
        End If
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click, Btn_Refresh.Click, Btn_Previous.Click, Btn_Next.Click, Btn_Last.Click, Btn_First.Click, Btn_Delete.Click, Btn_Add.Click
        Select Case CType(sender, Button).Name.ToString
            Case "Btn_Save"
                Save()
            Case "Btn_Refresh"
                acr.MoveNavi("Load", 0, Me, BS)
            Case "Btn_Previous"
                acr.MoveNavi("MovePrev", 0, Me, BS)
            Case "Btn_Next"
                acr.MoveNavi("MoveNext", 0, Me, BS)
            Case "Btn_Last"
                acr.MoveNavi("MoveLast", 0, Me, BS)
            Case "Btn_First"
                acr.MoveNavi("MoveFirst", 0, Me, BS)
            Case "Btn_Add"
                acr.MoveNavi("Add", 0, Me, BS)
            Case "Btn_Delete"
                Delete()
        End Select
    End Sub

    Private Sub Master_userDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Master_userDataGridView.CellClick
        ID_USER.Text = Master_userDataGridView.Rows(e.RowIndex).Cells(0).Value.ToString()
        USERNAME.Text = Master_userDataGridView.Rows(e.RowIndex).Cells(1).Value.ToString()
        PASSWORD.Text = Master_userDataGridView.Rows(e.RowIndex).Cells(2).Value.ToString()
        ROLE.Text = Master_userDataGridView.Rows(e.RowIndex).Cells(3).Value.ToString()
    End Sub

    Private Sub Master_sopirDataGridView_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles Master_userDataGridView.DataError
        e.Cancel = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ROLE.SelectedIndexChanged

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        LoadNow()
    End Sub

    Private Sub SIMPAN_BTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SIMPAN_BTN.Click
        Dim myConnString As String
        myConnString = "server=localhost;User Id=root;password=;Persist Security Info=True;database=db_petruk_new"
        Dim ds As New DataSet
        Dim dsconn As New MySqlConnection(myConnString)
        Dim strsql As String = ""

        Try
            If Form_Valid() = "" Then
                If ID_USER.Text = "" Or ID_USER.Text = "-1" Then
                    strsql = "INSERT INTO master_user (USERNAME,PASSWORD,ROLE) VALUES ('" & USERNAME.Text & "','" & PASSWORD.Text & "','" & ROLE.Text & "')"
                Else
                    strsql = "UPDATE master_user SET USERNAME = '" & USERNAME.Text & "', PASSWORD = '" & PASSWORD.Text & "',ROLE = '" & ROLE.Text & "' WHERE ID_USER = '" & ID_USER.Text & "'"
                End If
                Dim dsDa As New MySqlCommand(strsql, dsconn)
                dsconn.Open()
                dsDa.ExecuteNonQuery()
                Me.Validate()
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
                clearForm()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
    Sub clearForm()
        ID_USER.Text = ""
        USERNAME.Text = ""
        PASSWORD.Text = ""
        ROLE.Text = ""
    End Sub

    Private Sub Master_userDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Master_userDataGridView.CellContentClick

    End Sub
End Class